package com.cf.autoconfigure;

import com.cf.CommonProperty;
import com.cf.contraband.BannerFilter;
import com.cf.location.City;
import com.cf.mapper.BeanMapper;
import com.cf.mapper.DefaultBeanMapper;
import com.cf.ongl.OnglService;
import com.cf.spel.SpELService;
import com.cf.util.SpringBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.io.FileNotFoundException;

/**
 * game-server-BaseConfig
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/6/13 下午 06:56
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(CommonProperty.class)
public class CommonConfig {

    @Autowired
    private CommonProperty commonProperty;

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    public SpringBeanUtil springBeanUtil() {
        log.info("未找到自定义的SpringBeanUtil，实例化默认的SpringBeanUtil");
        return new SpringBeanUtil(applicationContext);
    }

    //########################工具#######################

    @Bean
    public BeanMapper beanMapper() {
        log.info("未找到自定义的BeanMapper，实例化默认的DefaultBeanMapper");
        return new DefaultBeanMapper();
    }


    @Bean
    public SpELService spELService() {
        log.info("未找到自定义的SpELService，实例化默认的SpELService");
        return new SpELService();
    }


    @Bean
    public BannerFilter bannerFilter() throws FileNotFoundException {
        log.info("未找到自定义的BannerFilter，实例化默认的BannerFilter");
        return new BannerFilter().init(commonProperty.getMgc());
    }

    @Bean
    public OnglService onglService() {
        log.info("未找到自定义的OnglService，实例化默认的OnglService");
        return new OnglService();
    }

    @Bean
    public City city() throws Exception {
        log.info("未找到自定义的City，实例化默认的City");
        return new City();
    }


}
